<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        .w {
            width: 1226px;
            margin: 0 auto;
        }

        .header {
            height: 150px;
            background: grey;
            overflow-y: scroll;
        }

        .sub {
            height: 1000px;
            width: 100px;
            background-color: #6ff;
        }

        .wrapNav {
            height: 45px;
        }

        .navBar {
            background-color: #ff0;
            width: 100%;
        }

        .fixed {
            position: fixed;
            left: 0;
            top: 0;
        }

        .nav {
            display: flex;
            height: 45px;
            line-height: 45px;
        }

        .nav li {
            flex: 1;
            border: 1px solid #000;
            text-align: center;
        }

        .nav li+li {
            border-left: 0;
        }

        .main .part {
            height: 400px;
            text-align: center;
            line-height: 400px;
            border: 1px solid #000;
        }

        .main .part+.part {
            border-top: 0;
        }
        .footer{
            height: 500px;
            background-color: grey;
        }

        .tips {
            width: 100%;
            line-height: 45px;
            position: fixed;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
            text-align: center;
            display: none;
        }

        .back {
            width: 45px;
            height: 45px;
            border: 1px solid #333;
            text-align: center;
            line-height: 45px;
            position: fixed;
            right: 5px;
            bottom: 50px;
            display: none;
        }
    </style>
</head>

<body>
    <div class="header w">
        <div class="sub"></div>
    </div>
    <div class="wrapNav">
        <div class="navBar">
            <ul class="nav w">
                <li><a href="#one">link_01</a></li>
                <li><a href="#two">llink_02</a></li>
                <li>link_03</li>
                <li>link_04</li>
                <li>link_05</li>
                <li>link_06</li>
            </ul>
        </div>
    </div>
    <div class="main w">
        <div id="one" class="part">part_01</div>
        <div id="two" class="part">part_02</div>
        <div class="part">part_03</div>
        <div class="part">part_04</div>
        <div class="part">part_05</div>
        <div class="part">part_06</div>
        <div class="part">part_07</div>
        <div class="part">part_08</div>
        <div class="part">part_09</div>
        <div class="part">part_10</div>
    </div>
    <div class="footer"></div>
    <div class="tips">到达底部</div>
    <div class="back">top</div>
</body>
<script>
    // 需求
    // 1. 导航栏吸顶
    // 2. 判断滚动条是否到达底部  => tips显隐
    // 3. 返回顶部

    var header = document.querySelector(".header");
    var navBar = document.querySelector(".navBar");
    var tips = document.querySelector(".tips");
    var back = document.querySelector(".back");
    console.log(header);

    var timer = null;
    var scrollTimer = null;

    // console.log(header.clientHeight, header.scrollHeight);  // 
    window.onscroll = function () {
        var scrollTop = document.documentElement.scrollTop;
        // console.log(scrollTop);

        // 滚动的最大值(页面上升的最大距离) = 文档的实际高度 - 文档的可视高度(一屏高)
        var scrollMax = document.documentElement.scrollHeight - document.documentElement.clientHeight;

        if (scrollTop >= header.clientHeight) {  // >= 150
            navBar.style.position = "fixed";
            navBar.style.top = "0";
        } else {
            navBar.style.position = "static";
        }

        // 问题:
        // 理想情况下 scrollTop === scrollMax  但是由于电脑默认设置的缩放比例原因 =>出现小数
        // 设置误差允许范围
        // scrollTop >= scrollMax - 2 &&  scrollTop <= scrollMax + 2
        // Math.abs(scrollTop - scrollMax)  > Math.abs(2)

        console.log(scrollMax, scrollTop);
        if (scrollTop >= scrollMax - 1 && scrollTop <= scrollMax + 1) {
            // 防抖处理
            clearTimeout(timer);
            timer = setTimeout(function () {
                console.log("到达底部");
                tips.style.display = "block";
                setTimeout(function () { // 等待0.5s表隐藏
                    tips.style.display = "none";
                }, 500)

            }, 300)
        }

        if (scrollTop >= 1000) {
            back.style.display = "block";
        } else {
            back.style.display = "none";
        }

    }

    back.onclick = function () {
        // document.documentElement.scrollTop = 0;
        // var scrollTop = document.documentElement.scrollTop; //获取滚动条的位置

        // clearInterval(scrollTimer);
        // scrollTimer = setInterval(function () {
        //     scrollTop -= 100;
        //     document.documentElement.scrollTop = scrollTop;
        //     if (scrollTop <= 0) {
        //         clearInterval(scrollTimer);
        //     }
        // }, 10)


        window.scrollTo({
            top: 0,
            behavior: "smooth",
        })

    }
</script>

</html>